package com.yikaiye.android.yikaiye.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.DialogChooseTimeBinding;
import com.yikaiye.android.yikaiye.utils.DateUtil;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

/**
 * 时间范围选择弹窗
 */
public abstract class ChooseTimeDialog {
    private final DialogChooseTimeBinding binding;
    private final Dialog dialog;
    private final Context context;
    private TimePicker timePicker;
    private boolean isStartTime;

    public ChooseTimeDialog(Context context) {
        this.context = context;
        initDialog();
        binding = DialogChooseTimeBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
            // lp.x = 266; // 新位置X坐标
            // lp.y = Utils.dip2px(activity, 300); // 新位置Y坐标
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            dialogWindow.setAttributes(lp);
        }
        binding.llChooseOne.setOnClickListener(view1 -> {
            isStartTime = true;
            timePicker.setPickerViewTitle(context.getString(R.string.choose_start_time));
            timePicker.showPickerView();
        });

        binding.llChooseTwo.setOnClickListener(view2 -> {
            isStartTime = false;
            timePicker.setPickerViewTitle(context.getString(R.string.choose_end_time));
            timePicker.showPickerView();
        });

        binding.tvCommit.setOnClickListener(v -> {
            if (TextUtils.isEmpty(binding.tvStartTime.getText().toString())) {
                ToastHelper.show(context.getString(R.string.choose_start_time));
                return;
            }
            if (TextUtils.isEmpty(binding.tvEndTime.getText().toString())) {
                ToastHelper.show(context.getString(R.string.choose_end_time));
                return;
            }
            String startTime = binding.tvStartTime.getText().toString().replace("-", ".");
            String endTime = binding.tvEndTime.getText().toString().replace("-", ".");
            if (DateUtil.getStringToDate(binding.tvStartTime.getText().toString())
                    > DateUtil.getStringToDate(binding.tvEndTime.getText().toString())) {
                ToastHelper.show(context.getString(R.string.start_time_greater_than_end_time));
                return;
            }

            confirm(startTime, endTime);
            dismissDialog();
        });
    }

    public abstract void confirm(String startTime, String endTime);


    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }

    private void initDialog() {
        timePicker = new TimePicker(context, true) {
            @Override
            public void confirm(String date) {
                if (isStartTime) {
                    binding.tvStartTime.setText(date);
                } else {
                    binding.tvEndTime.setText(date);
                }
            }
        };
    }
}
